package com.sailing.xjpb.common;

import java.util.Collection;
import java.util.Map;

/**
 * 字节码工具类
 */
public class ClassUtils {

    private ClassUtils() {

    }

    /**
     * 是否是数组
     */
    public static boolean isMapType(Class<?> clazz) {
        return Map.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是数组
     */
    public static boolean isArrayType(Class<?> clazz) {
        return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是String
     */
    public static boolean isStringType(Class<?> clazz) {
        return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是int
     */
    public static boolean isNumberType(Class<?> clazz) {
        return Number.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是boolean
     */
    public static boolean isBooleanType(Class<?> clazz) {
        return clazz.equals(Boolean.class);
    }


    /**
     * Change primitive Class types to the associated wrapper class. This is
     * useful for concrete converter implementations which typically treat
     * primitive types like their corresponding wrapper types.
     *
     * @param <T>  The type to be checked.
     * @param type The class type to check.
     * @return The converted type.
     * @since 1.9
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> primitiveToWrapper(final Class<T> type) {
        if (type == null || !type.isPrimitive()) {
            return type;
        }

        if (type == Integer.TYPE) {
            return (Class<T>) Integer.class;
        } else if (type == Double.TYPE) {
            return (Class<T>) Double.class;
        } else if (type == Long.TYPE) {
            return (Class<T>) Long.class;
        } else if (type == Boolean.TYPE) {
            return (Class<T>) Boolean.class;
        } else if (type == Float.TYPE) {
            return (Class<T>) Float.class;
        } else if (type == Short.TYPE) {
            return (Class<T>) Short.class;
        } else if (type == Byte.TYPE) {
            return (Class<T>) Byte.class;
        } else if (type == Character.TYPE) {
            return (Class<T>) Character.class;
        } else {
            return type;
        }
    }
}
